/**
 * Created by Richard Radlherr.
 * Date: 30.06.2006 18:08
 */

using System;
using TagTool.Core;
using TagTool.Devices.Joystick;
using TagTool.Devices;
using TagTool.GraphicTypes;

using System.Windows.Forms;

namespace TagTool.Nodes.Devices
{

	public class JoystickNode : BaseNode
	{
		
		private OutputSlot x;
		private OutputSlot y;
		private OutputSlot twist;
		private OutputSlot throttle;

        private OutputSlot location;

		private OutputSlot btn1;
		private OutputSlot btn2;
		private OutputSlot btn3;
		private OutputSlot btn4;
		private OutputSlot btn5;
		private OutputSlot btn6;
		private OutputSlot btn7;
		private OutputSlot btn8;

        JoystickDevice device;
        static JoystickManager manager = new JoystickManager();

		public JoystickNode()
		{
            location = new OutputSlot(this, "Location", typeof(PointD));
            btn1 = new OutputSlot(this, "Button 1", typeof(bool));
            btn2 = new OutputSlot(this, "Button 2", typeof(bool));
            btn3 = new OutputSlot(this, "Button 3", typeof(bool));
            btn4 = new OutputSlot(this, "Button 4", typeof(bool));
            btn5 = new OutputSlot(this, "Button 5", typeof(bool));
            btn6 = new OutputSlot(this, "Button 6", typeof(bool));
            btn7 = new OutputSlot(this, "Button 7", typeof(bool));
            btn8 = new OutputSlot(this, "Button 8", typeof(bool));
            x = new OutputSlot(this, "X", typeof(Double));
            y = new OutputSlot(this, "Y", typeof(Double));
            twist = new OutputSlot(this, "Twist", typeof(Double));
            throttle = new OutputSlot(this, "Throttle", typeof(Double));

            btn1.Value = false;
            btn2.Value = false;
            btn3.Value = false;
            btn4.Value = false;
            btn5.Value = false;
            btn6.Value = false;
            btn7.Value = false;
            btn8.Value = false;

            device = (JoystickDevice)manager.GetDevice(0);
            if (device != null)
            {
                device.JoystickEvent += new JoystickEventHandler(OnJoystickEvent);
                device.Connect();

                this.Name = "Joystick";
                location.Value = device.Location;
                x.Value = device.X;
                y.Value = device.Y;
                twist.Value = 0.0;
                throttle.Value = device.Z;
            }
            else
            {
                this.Name = "Joystick (not present)";
                location.Value = new PointD();
                x.Value = 0.0;
                y.Value = 0.0;
                twist.Value = 0.0;
                throttle.Value = 0.0;
            }

		}
		
        private void OnJoystickEvent(Object sender, JoystickDeviceEventArgs e)
        {
            location.Value = new PointD(e.Location.X / 327.67 - 100.0, e.Location.Y / 327.67 - 100.0, 0.0);
            x.Value = ((PointD)location.Value).X;
            y.Value = ((PointD)location.Value).Y;
            throttle.Value = ((PointD)e.Rudder).X / 327.67 - 100.0;
            twist.Value = ((PointD)e.Rudder).Y / 327.67 - 100.0;

            btn1.Value = (e.Buttons & 1) != 0;
            btn2.Value = (e.Buttons & 2) != 0;
            btn3.Value = (e.Buttons & 4) != 0;
            btn4.Value = (e.Buttons & 8) != 0;
            btn5.Value = (e.Buttons & 16) != 0;
            btn6.Value = (e.Buttons & 32) != 0;
            btn7.Value = (e.Buttons & 64) != 0;
            btn8.Value = (e.Buttons & 128) != 0;
        }

		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot X
		{
			get { return x; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Y
		{
			get { return y; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Twist
		{
			get { return twist; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Throttle
		{
			get { return throttle; }
		}

        [NodeAttribute(SlotType.Output, Show = true)]
        public OutputSlot Location
        {
            get { return location; }
        }
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn1
		{
			get { return btn1; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn2
		{
			get { return btn2; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn3
		{
			get { return btn3; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn4
		{
			get { return btn4; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn5
		{
			get { return btn5; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn6
		{
			get { return btn6; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn7
		{
			get { return btn7; }
		}
		
		[NodeAttribute(SlotType.Output, Show = true)]
		public OutputSlot Btn8
		{
			get { return btn8; }
		}
		
		public override void Update(Object sender)
		{
		}


	}
}
